import os
import cv2
import numpy as np
import matplotlib.pyplot as plt
from utils import *
path = "../img/"
plt.rcParams['image.cmap'] = 'gray'
plt.rcParams["figure.dpi"] = 300
plt.rcParams["figure.figsize"] = (6,6)
img = cv2.imread(os.path.join(path, 'lena.png'))
plt.imshow(bgr2rgb(img))
plt.show()
img = cv2.imread(os.path.join(path, 'lena.png'))
height, width, = img.shape[:2]
quarter_height, quarter_width = height/4, width/4
We use warpaffine to transform the image using matrix
T = $ \left[ \begin{array}{ccc} 1 & 0 & T_x \\ 0 & 1 & T_y \end{array} \right] $
T is our transformation matrix
T = np.float32([[1, 0, quarter_width],
[0, 1, quarter_height]])
img_translation = cv2.warpAffine(img, T, (width,height))
plt.figure(figsize=(16,4))
plt.subplot(121); plt.title("Original"); plt.imshow(cv2.cvtColor(img,cv2.COLOR_BGR2RGB))
plt.subplot(122); plt.title("Translated"); plt.imshow(cv2.cvtColor(img_translation,cv2.COLOR_BGR2RGB))
plt.show()
R = $ \left[ \begin{array}{ccc} \cos\theta & -\sin\theta \\ \sin\theta & \cos\theta \end{array} \right] $
Where theta is the angle of rotation
image = cv2.imread(os.path.join(path, 'lena.png'))
height, width = image.shape[:2]
theta = 60
rotation_matrix = cv2.getRotationMatrix2D((width/2, height/2), theta, .5)
rotation_matrix
array([[ 0.35355339, 0.35355339, 74.98066402],
[ -0.35355339, 0.35355339, 256. ]])
img_rotated = cv2.warpAffine(image, rotation_matrix,(width, height))
plt.figure(figsize=(16,4))
plt.subplot(121); plt.title("Original"); plt.imshow(bgr2rgb(img))
plt.subplot(122); plt.title("Rotation"); plt.imshow(bgr2rgb(img))
plt.show()
image = cv2.imread(os.path.join(path, 'lena.png'))
# Making a square
square = np.zeros((300,300), np.uint8)
cv2.rectangle(square, (50,50), (250,250), 255, -2)
plt.imshow(square)
plt.show()
#making ellipse
ellipse = np.zeros((300,300), np.uint8)
cv2.ellipse(ellipse, (150,150), (150,150), 30, 0, 180, 255, -1)
plt.imshow(ellipse)
plt.show()
#show only where they intersectd
And = cv2.bitwise_and(square, ellipse)
plt.imshow(And)
plt.show()
bitwiseOr = cv2.bitwise_or(square, ellipse)
plt.imshow(bitwiseOr)
plt.show()
bitwiseXor = cv2.bitwise_xor(square, ellipse)
plt.imshow(bitwiseXor)
plt.show()
bitwiseNot_sq = cv2.bitwise_not(square)
plt.imshow(bitwiseNot_sq)
plt.show()